package net.oschina.git.fsppp.manager;


import net.oschina.git.fsppp.dao.DirDao;
import net.oschina.git.fsppp.model.P3Dir;
import net.oschina.git.fsppp.model.SimpleResponse;
import net.oschina.git.fsppp.utils.P3Utils;

import java.io.IOException;
import java.util.List;

/**
 * 目录管理类
 * Created by guin_guo on 2016/10/8.
 */
public class DirManager implements IDirManager{

    protected DirDao dirDao;

    public DirManager() {
        this.dirDao = DirDao.getInstance();
    }

    public static class DefaultInstance {
        public static DirManager instance = new DirManager();
    }
    public static DirManager getInstance() {
        return DefaultInstance.instance;
    }

    /**
     * 创建文件夹
     * @param path 路径 c:\
     * @param name 文件夹名称  java
     * @return
     * SimpleResponse
     *      - msg:      "创建文件夹 name 成功"
     *      - status:   TRUE
     *      - entity:   Dir
     *                      - name: java
     *                      - path c:\java
     *                      ...
     */
    @Override
    public SimpleResponse<P3Dir> createDir(String path, String name) {
        SimpleResponse<P3Dir> simpleResponse = new SimpleResponse<>();
        try {
            P3Dir dir = dirDao.create(path, name);
            simpleResponse.setEntity(dir);
            simpleResponse.setStatus(Boolean.TRUE);
            simpleResponse.setMsg("创建文件夹[" + name + "]成功");
        } catch (Exception e) {
            e.printStackTrace();
            simpleResponse.setStatus(Boolean.FALSE);
            simpleResponse.setMsg(e.getMessage());
        }
        return simpleResponse;
    }

    /**
     * 获取文件夹下的子文件夹,子文件
     * 只获取一层
     * @param dir
     * @return
     * @throws IOException
     */
    @Override
    public SimpleResponse<List<P3Dir>> listDir(P3Dir dir) {
        SimpleResponse<List<P3Dir>> simpleResponse = new SimpleResponse<>();
        try {
            List<P3Dir> dirs = dirDao.listByParent(dir);
            simpleResponse.setStatus(Boolean.TRUE);
            simpleResponse.setMsg("success");
            simpleResponse.setEntity(dirs);
        } catch (Exception e) {
            e.printStackTrace();
            simpleResponse.setStatus(Boolean.FALSE);
            simpleResponse.setMsg(e.getMessage());
        }
        return simpleResponse;
    }

    /**
     * 删除空目录，尚未检查目录下的文件
     * startNum=0表示已删除
     * @param dir
     * @return
     * @throws IOException
     */
    @Override
    public SimpleResponse<P3Dir> deleteNullDir(P3Dir dir) {
        SimpleResponse<P3Dir> simpleResponse = new SimpleResponse<>();
        try {
            List<P3Dir> childs = dirDao.listByParent(dir);
            if (childs.size() != 0) {
                simpleResponse.setStatus(Boolean.FALSE);
                simpleResponse.setMsg(dir.getPath() + " 目录不为空");
                return simpleResponse;
            }
            String diskName = dir.getPath().substring(0,1);
            dirDao.deleteDirById(diskName, dir.getStartNum());
        } catch (Exception e) {
            e.printStackTrace();
            simpleResponse.setStatus(Boolean.FALSE);
            simpleResponse.setMsg(e.getMessage());
            return simpleResponse;
        }
        dir.setStartNum(0);
        simpleResponse.setStatus(Boolean.TRUE);
        simpleResponse.setMsg("success");
        simpleResponse.setEntity(dir);
        return simpleResponse;
    }
    /**
     * 删除非空目录
     * startNum=0表示已删除
     * @param dir
     * @return
     * @throws IOException
     */
    public SimpleResponse<P3Dir> deleteDir(P3Dir dir) {
        SimpleResponse<P3Dir> simpleResponse = new SimpleResponse<>();
        try {
            dirDao.deleteDir(dir);
        } catch (Exception e) {
            e.printStackTrace();
            simpleResponse.setStatus(Boolean.FALSE);
            simpleResponse.setMsg(e.getMessage());
            return simpleResponse;
        }
        dir.setStartNum(0);
        simpleResponse.setStatus(Boolean.TRUE);
        simpleResponse.setMsg("success");
        simpleResponse.setEntity(dir);
        return simpleResponse;
    }

    /**
     * 根据
     * 磁盘，文件
     * 分配表id获取Dir文件(夹)
     * @param diskName
     * @param id
     * @return
     * @throws IOException
     */
    @Override
    public SimpleResponse<P3Dir> getById(String diskName, Integer id) {
        SimpleResponse<P3Dir> simpleResponse = new SimpleResponse<>();
        try {
            P3Dir dir = P3Utils.getById(diskName, id);
            simpleResponse.setMsg("success");
            simpleResponse.setEntity(dir);
        } catch (Exception e) {
            e.printStackTrace();
            simpleResponse.setStatus(Boolean.FALSE);
            simpleResponse.setMsg(e.getMessage());
            return simpleResponse;
        }
        simpleResponse.setStatus(Boolean.TRUE);
        return simpleResponse;
    }
    /**
     * 根据 绝对路径获取Dir文件(夹)
     * @param path 绝对路径
     * @return
     * @throws IOException
     */
    @Override
    public SimpleResponse<P3Dir> getByPath(String path) {
        SimpleResponse<P3Dir> simpleResponse = new SimpleResponse<>();
        try {
            P3Dir dir = P3Utils.getByPath(path);
            simpleResponse.setMsg("success");
            simpleResponse.setEntity(dir);
        } catch (Exception e) {
            e.printStackTrace();
            simpleResponse.setStatus(Boolean.FALSE);
            simpleResponse.setMsg(e.getMessage());
            return simpleResponse;
        }
        simpleResponse.setStatus(Boolean.TRUE);
        return simpleResponse;
    }

    @Override
    public SimpleResponse<List<P3Dir>> listByPath(String path) {
        SimpleResponse<List<P3Dir>> simpleResponse = new SimpleResponse<>();
        try {
            List<P3Dir> dirs =dirDao.listByPath(path);
            if (dirs != null) {
                simpleResponse.setMsg("success");
                simpleResponse.setEntity(dirs);
            } else {
                throw new NullPointerException("目录不存在");
            }
        } catch (Exception e) {
            simpleResponse.setStatus(Boolean.FALSE);
            simpleResponse.setMsg(e.getMessage());
            return simpleResponse;
        }
        simpleResponse.setStatus(Boolean.TRUE);
        return simpleResponse;
    }

    @Override
    public SimpleResponse<List<P3Dir>> search(String path, String key) {
        SimpleResponse<List<P3Dir>> simpleResponse = new SimpleResponse<>();
        try {
            P3Dir current =dirDao.getByPath(path);
            if (current != null) {
                List<P3Dir> dirs = dirDao.search(current, key);
                simpleResponse.setMsg("success");
                simpleResponse.setEntity(dirs);
            } else {
                throw new NullPointerException("目录不存在");
            }
        } catch (Exception e) {
            simpleResponse.setStatus(Boolean.FALSE);
            simpleResponse.setMsg(e.getMessage());
            return simpleResponse;
        }
        simpleResponse.setStatus(Boolean.TRUE);
        return simpleResponse;
    }

}
